package mainFragment.fragment;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;

import java.util.ArrayList;
import java.util.List;

import common.BaseFragment;
import mainFragment.activity.CollegeMoreDetail;
import mainFragment.adapter.CollegeIntroduceAdapter;
import mainFragment.bean.CollegeIndexBean;
import utils.CalculateUtils;
import utils.ShowImageUtils;
import view.MyImageLoader;
import view.MyListView;

/**
 * Created by Administrator on 2017/3/3.
 * 高校简介
 */
@SuppressLint("ValidFragment")
public class CollegeIntroduce extends BaseFragment implements View.OnClickListener {
    private View view;
    private Context context;
    private MyListView college_introduce_bestmajor_lv;
    private CollegeIntroduceAdapter collegeIntroduceAdatper;
    private LinearLayout college_introduce_introduce_layout;//学校简介
    private TextView college_introduce_introduce_tv;//学校简介
    private ImageView college_introduce_introduce_ima;//更多简介
    private boolean showMore = false;//更多简介
    private ArrayList<CollegeIndexBean.MajorListBean> arrayList = new ArrayList<>();
    private ArrayList<String> majors = new ArrayList<>();
    private Handler handler;
    private LinearLayout college_introduce_ima_layout;
    private ImageView college_introduce_img1;
    private ImageView college_introduce_img2;
    private RelativeLayout college_introduce_img3_layout;
    private ImageView college_introduce_img3;
    private TextView college_introduce_img3_cover;
    private List<String> images;
    private ShowImageUtils showImageUtils;

    public CollegeIntroduce() {

    }

    public CollegeIntroduce(Context context, Handler handler) {
        this.context = context;
        this.handler = handler;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.college_introduce, null, false);
        }
        Message ms = new Message();
        ms.what = CollegeMoreDetail.INTRODUCE_COMPLETE;
        handler.sendMessage(ms);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (getMyActivity() != null) {
            initView();
        }
        return view;
    }

    private void initView() {
        college_introduce_introduce_tv = (TextView) view.findViewById(R.id.college_introduce_introduce_tv);
        college_introduce_introduce_ima = (ImageView) view.findViewById(R.id.college_introduce_introduce_ima);
        college_introduce_introduce_layout = (LinearLayout) view.findViewById(R.id.college_introduce_introduce_layout);
        college_introduce_introduce_layout.setOnClickListener(this);
        college_introduce_img1 = (ImageView) view.findViewById(R.id.college_introduce_img1);
        college_introduce_img2 = (ImageView) view.findViewById(R.id.college_introduce_img2);
        college_introduce_img3 = (ImageView) view.findViewById(R.id.college_introduce_img3);
        college_introduce_img1.setOnClickListener(this);
        college_introduce_img2.setOnClickListener(this);
        college_introduce_img3_layout = (RelativeLayout) view.findViewById(R.id.college_introduce_img3_layout);
        college_introduce_img3_layout.setOnClickListener(this);
        college_introduce_img3_cover = (TextView) view.findViewById(R.id.college_introduce_img3_cover);
        college_introduce_bestmajor_lv = (MyListView) view.findViewById(R.id.college_introduce_bestmajor_lv);
        collegeIntroduceAdatper = new CollegeIntroduceAdapter(getMyActivity(), arrayList);
        college_introduce_bestmajor_lv.setAdapter(collegeIntroduceAdatper);
        college_introduce_bestmajor_lv.setFocusable(false);
        college_introduce_ima_layout = (LinearLayout) view.findViewById(R.id.college_introduce_ima_layout);
        CalculateUtils.calculateListViewHeight(college_introduce_bestmajor_lv, collegeIntroduceAdatper);
        showImageUtils = new ShowImageUtils();
    }

    public void setCollegeIndexBean(CollegeIndexBean collegeIndexBean) {
        if (collegeIndexBean != null) {
            CollegeIndexBean.CollegeBean college = collegeIndexBean.getCollege();
            List<CollegeIndexBean.MajorListBean> majorList = collegeIndexBean.getMajorList();
            if (college != null) {
                college_introduce_introduce_tv.setText(college.getIntro());
                images = college.getImages();
                if (images != null) {
                    if (images.size() > 3) {
                        int i = images.size() - 3;
                        college_introduce_img3_cover.setVisibility(View.VISIBLE);
                        college_introduce_img3_cover.setText("+" + i);
                        college_introduce_img3.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                showImageUtils.showImageUtils(getMyActivity(), college_introduce_img3, images, 2);
                            }
                        });
                    } else {
                        college_introduce_img3_cover.setVisibility(View.GONE);
                        college_introduce_img3.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                showImageUtils.showImageUtils(getMyActivity(), college_introduce_img3, images, 2);
                            }
                        });
                    }
                    if (images.size() >= 3) {
                        MyImageLoader.showImage(images.get(0), college_introduce_img1);
                        MyImageLoader.showImage(images.get(1), college_introduce_img2);
                        MyImageLoader.showImage(images.get(2), college_introduce_img3);
                        final float scale = (float) 10 / (float) 11;//宽比高
                        ViewTreeObserver vto = college_introduce_img1.getViewTreeObserver();

                        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                            @Override
                            public void onGlobalLayout() {
                                college_introduce_img1.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                                int width = college_introduce_img1.getMeasuredWidth();
                                college_introduce_img1.setLayoutParams(new LinearLayout.LayoutParams(width, (int) ((float) width * scale), 1));
                                college_introduce_img2.setLayoutParams(new LinearLayout.LayoutParams(width, (int) ((float) width * scale), 1));
                                college_introduce_img3_layout.setLayoutParams(new LinearLayout.LayoutParams(width, (int) ((float) width * scale), 1));

                            }
                        });
                    } else if (images.size() == 2) {
                        MyImageLoader.showImage(images.get(0), college_introduce_img1);
                        MyImageLoader.showImage(images.get(1), college_introduce_img2);
                        college_introduce_img3_layout.setVisibility(View.GONE);
                        ViewTreeObserver vto = college_introduce_img1.getViewTreeObserver();
                        final float scale = (float) 10 / (float) 11;//宽比高
                        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                            @Override
                            public void onGlobalLayout() {
                                college_introduce_img1.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                                int width = college_introduce_img1.getMeasuredWidth();
                                college_introduce_img1.setLayoutParams(new LinearLayout.LayoutParams(width, (int) ((float) width * scale), 1));
                                college_introduce_img2.setLayoutParams(new LinearLayout.LayoutParams(width, (int) ((float) width * scale), 1));
                            }
                        });


                    } else if (images.size() == 1) {
                        MyImageLoader.showImage(images.get(0), college_introduce_img1);
                        college_introduce_img3_layout.setVisibility(View.GONE);
                        ViewTreeObserver vto = college_introduce_img1.getViewTreeObserver();
                        final float scale = (float) 10 / (float) 11;//宽比高
                        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                            @Override
                            public void onGlobalLayout() {
                                college_introduce_img1.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                                int width = college_introduce_img1.getMeasuredWidth();
                                college_introduce_img1.setLayoutParams(new LinearLayout.LayoutParams(width, (int) ((float) width * scale), 1));
                            }
                        });
                    } else {
                        college_introduce_ima_layout.setVisibility(View.GONE);
                    }
                }
            }
            if (majorList != null) {
                for (int i = 0; i < majorList.size(); i++) {
                    if (!majors.contains(majorList.get(i).getMajor())) {
                        majors.add(majorList.get(i).getMajor());
                        arrayList.add(majorList.get(i));
                    }
                }
                collegeIntroduceAdatper.notifyDataSetChanged();
                CalculateUtils.calculateListViewHeight(college_introduce_bestmajor_lv, collegeIntroduceAdatper);
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        showImageUtils = null;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.college_introduce_introduce_layout:
                if (!showMore) {
                    if (college_introduce_introduce_tv.getLineCount() >= 3) {
                        college_introduce_introduce_tv.setMaxLines(999);
                        college_introduce_introduce_ima.setImageResource(R.mipmap.hidden_arrow);
                        showMore = true;
                    }
                } else {
                    college_introduce_introduce_tv.setMaxLines(3);
                    college_introduce_introduce_ima.setImageResource(R.mipmap.volunteer_more_introduction_ima);
                    showMore = false;
                }
                break;
            case R.id.college_introduce_img3_layout:
                break;
            case R.id.college_introduce_img1:
                showImageUtils.showImageUtils(getMyActivity(), college_introduce_img1, images, 0);
                break;
            case R.id.college_introduce_img2:
                showImageUtils.showImageUtils(getMyActivity(), college_introduce_img2, images, 1);
                break;
            default:
                break;
        }
    }
}
